<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   
<%@page import="constants.StringConstants" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Update Dish</title>

<script type="text/javascript">
function pick_food() {
	var dishId = '${requestScope.DISHDTO.id}';
	window.open("DishMaintainServlet?action=<%=StringConstants.EDIT_FOOD_TO_DISH%>&dishId="+ dishId ,"_blank","toolbar=yes, scrollbars=yes, resizable=yes, top=100, left=200, width=400, height=400");
}

function validate() {
	var name = document.getElementById("name").value;
	var calo = document.getElementById("calo").value;
	var errorSpan = document.getElementById("error");
	var regex = /^\d+$/;
	
	if (name == "" || calo == "") {
		errorSpan.innerHTML = "<strong><font color='red'>Please input name and calo</font></strong>";
		errorSpan.style.visibility = 'visible';
		return false;
	}
	
	if (regex.test(calo)){
		if (calo < 300 || calo > 3000) {
			errorSpan.innerHTML = "<strong><font color='red'>Calo out of range</font></strong>";
			errorSpan.style.visibility = 'visible';
			return false;
		}
	}	
	return true;
}
</script>

</head>
<body>
<span style="float:left"><a href="/XMLProject/admin/admin-home.jsp">Go to Home</a></span><br/>
<span style="float:right">
	Hi, <strong>${sessionScope.ADMIN_USERNAME}</strong>-
	<a href="AccountServlet?action=<%=StringConstants.LOGOUT%>">Logout</a>
</span>
<h2>Update Dish</h2>
<c:set var="dishDTO" value="${requestScope.DISHDTO}"></c:set>
	<form action="DishMaintainServlet" method="post" enctype="multipart/form-data" onsubmit="return validate()">
	<table>
	<tr>
		<td>Dish*</td> <td><input type="text" id="name" name="name" value="${dishDTO.name}"/></td>
	</tr>
	<tr>
		<td>Calo*</td> <td><input type="text" id="calo" name="calo" value="${dishDTO.calo}"/> (range value 300 to 3000) </td>
	</tr>
	<tr>
		<td>Cooking</td>
		<td> 
		<c:set var="cookingList" value="${requestScope.COOKINGLIST}"/>
		<select name="cookingId">
			<c:forEach var="item" items="${cookingList}">
				<c:set var="itemId" value="${item.id}"/>
				<c:set var="dishId" value="${dishDTO.cooking.id}"/>
				<c:choose>
					<c:when test="${ itemId == dishId}">
						<option value="${item.id}" selected="selected">${item.name}</option>
					</c:when>
					<c:when test="${ itemId != dishId}">
						<option value="${item.id}">${item.name}</option>
					</c:when>
				</c:choose>
			</c:forEach>
		</select>
		</td>
	</tr>
	</table>
		Image: <img src="${dishDTO.imgPath}"/> 
		<input type="file" name="img" value=""/><br/>
		Description <br/>
		<textarea rows="5" cols="100" name="description"></textarea> <br/>
		<input type="button" onclick="pick_food()" value="Choose food material"/><br/><br/>
		<input type="hidden" name="id" value="${dishDTO.id}"/>
		<input type="hidden" id="foods_str" name="foods_str"/>
		<input type="submit" name="action" value="<%=StringConstants.CHANGE%>"><br/>
		<span id="error" style="visibility: hidden;"></span> 
	</form>
</body>
</html>